/**
 * 
 * Copyright (C) 2011 Cody Stoutenburg . All rights reserved.
 *
 *       This program is free software; you can redistribute it and/or
 *       modify it under the terms of the GNU Lesser General Public License
 *       as published by the Free Software Foundation; either version 2.1
 *       of the License, or (at your option) any later version.
 *
 *       This program is distributed in the hope that it will be useful,
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *       GNU Lesser General Public License for more details.
 *
 *       You should have received a copy of the GNU Lesser General Public License
 *       along with this program; if not, write to the Free Software
 *       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 * 
 */
package registry;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RMISecurityManager;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;

/**
 * @author Cody Stoutenburg
 * 
 */
public class RmiRegistryProxy extends UnicastRemoteObject
		implements
			IRmiRegistryProxy {

	private static final long serialVersionUID = -8432259884228815800L;

	private final String bindName;

	/**
	 * start an rmi server proxy so you can bind, unbind, rebind, ... from
	 * external host
	 * 
	 * @param args
	 *            [security file path [rmiregistry port]]
	 */
	public static void main(String[] args) {

		Integer port = 1099;
		String path = "C:/security.txt";
		if (args.length > 0) {
			path = args[0];
		} else if (args.length > 1) {
			path = args[0];
			port = Integer.valueOf(args[1]);
		} else {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					System.in));

			try {
				// Set the security depending on the file
				System.out.println("Donnez le path du fichier security:");
				path = reader.readLine();

				System.out
						.println("Donnez le port du rmiregistry (default 1099)");
				port = Integer.valueOf(reader.readLine());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		System.setProperty("java.security.policy", path);
		System.out.println(System.getProperty("java.security.policy"));
		// Create and install a security manager
		if (System.getSecurityManager() == null) {
			System.setSecurityManager(new RMISecurityManager());
		}
		System.out.println("registry created at port :" + port);

		try {
			LocateRegistry.createRegistry(port);

			RmiRegistryProxy proxy = new RmiRegistryProxy("//localhost:" + port
					+ "/" + IRmiRegistryProxy.RMI_NAME);
			Naming.rebind(proxy.getName(), proxy);

			System.out.println("server ready");
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
	}
	/**
	 * @throws RemoteException
	 */
	protected RmiRegistryProxy(String bindName) throws RemoteException {
		super();
		this.bindName = bindName;
	}

	public String getName() {
		return bindName;
	}

	@Override
	public Remote lookup(String name) throws RemoteException {
		Remote remote = null;
		try {
			remote = Naming.lookup(name);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (NotBoundException e) {
			e.printStackTrace();
		}

		return remote;
	}

	@Override
	public void bind(String name, Remote obj) throws RemoteException {
		try {
			Naming.bind(name, obj);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (AlreadyBoundException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void unbind(String name) throws RemoteException {
		try {
			Naming.unbind(name);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (NotBoundException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void rebind(String name, Remote obj) throws RemoteException {
		try {
			Naming.rebind(name, obj);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public String[] list(String name) throws RemoteException {
		String[] lst = {};
		try {
			lst = Naming.list(name);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		return lst;
	}

}
